﻿using System;
using System.Collections.Generic;
using System.Text;
using Mars.EnCor.ObjectBuilder;

namespace Mars.EnCor.Security
{
    
    public class MemoryCachedTokenProvider : TokenProvider
    {
        private Dictionary<string, EnCorIdentity> _Dictionary = new Dictionary<string, EnCorIdentity>();
        /// <summary>
        /// Generates the token.
        /// </summary>
        /// <param name="identity">The identity.</param>
        /// <returns></returns>
        public override string GenerateToken(EnCorIdentity identity)
        {
            Guid token = Guid.NewGuid();
            string tokenString = token.ToString();
            _Dictionary.Add(tokenString, identity);
            return tokenString;
        }

        /// <summary>
        /// Gets the identityby token.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public override EnCorIdentity GetIdentitybyToken(string token)
        {
            if (_Dictionary.ContainsKey(token))
            {
                return _Dictionary[token];
            }
            return null;
        }
    }

    [Assembler(typeof(MemoryCachedTokenProviderAssembler))]
    public class MemoryCachedTokenProviderConfig : TokenProviderConfig
    { 
        
    }

    public class MemoryCachedTokenProviderAssembler : IAssembler<ITokenProvider, TokenProviderConfig>
    {
        #region IAssembler<ITokenProvider,TokenProviderConfig> Members

        public ITokenProvider Assemble(IBuilderContext context, TokenProviderConfig objectConfiguration)
        {
            return new MemoryCachedTokenProvider();
        }

        #endregion
    }

}
